package vip.xiaonuo.megvii.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.timer.TimerTaskRunner;
import vip.xiaonuo.megvii.service.DeviceMegviiConfigService;
import vip.xiaonuo.modular.devicemegvii.entity.DeviceMegvii;
import vip.xiaonuo.modular.devicemegvii.service.DeviceMegviiService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 旷视设备的心跳
 */
@Component
@Slf4j
public class MegviiHeartBeatTask implements TimerTaskRunner {

    @Resource
    private DeviceMegviiService megviiService;

    @Resource
    private DeviceMegviiConfigService configService;

    @Override
    public void action(String jobParams,String taskId) {
        aiBoxHeartBeat();
    }

    @Override
    public void executeXxlJob() throws InterruptedException {
        aiBoxHeartBeat();
    }

    private void aiBoxHeartBeat() {
        List<DeviceMegvii> devices = megviiService.list();
        for(DeviceMegvii device : devices){
            if(System.currentTimeMillis() - device.getCreateTime().getTime() < 80000 ){
                continue;
            }
            ResponseData data = configService.check(device,true);
            if(!data.getSuccess()){
                log.info("AI盒子{} 心跳异常,原因：{} ",device.getMachName(),data.getMessage());
            }
        }
    }
}
